using System;
using System.Collections.Generic;
using UnityEngine;

namespace BTEditor
{
    [Serializable]
    public class BTLineTree
    {
        public Dictionary<BTNode, BTLine> BTLineDic;

        public BTLineTree()
        {
            BTLineDic = new Dictionary<BTNode, BTLine>();
        }

        public void DrawLines()
        {
            Event currentEvent = BTEditorProperty.Instance.Event;
            BTEditorProperty.Instance.RemoveNotUseLine();
            GUIStyle style = new GUIStyle();
            style.normal.background = BTNodeStyle.LineBoxTex as Texture2D;
            foreach (KeyValuePair<BTNode,BTLine> keyValuePair in BTLineDic)
            {
                BTNode node = keyValuePair.Key;
                BTLine line = keyValuePair.Value;
                List<BTNode> childs = line.ChildList;

                for (int i = 0; i < childs.Count; i++)
                {
                    BTNode child = childs[i];
                    BTUtils.DrawBezier(node.DownPointRect, child.UpDownRect);
                    GUI.DrawTexture(node.DownPointRect,BTNodeStyle.LinePoint);
                    GUI.DrawTexture(child.UpDownRect,BTNodeStyle.LinePoint);
                    Vector2 center = (node.DownPointRect.center + child.UpDownRect.center) * 0.5f;
                    Rect lintDisable = new Rect(center.x - BTEditorConst.LINE_DISABLE_LENGTH/2, center.y - BTEditorConst.LINE_DISABLE_LENGTH/2, 
                        BTEditorConst.LINE_DISABLE_LENGTH, BTEditorConst.LINE_DISABLE_LENGTH);
                    if (lintDisable.Contains(currentEvent.mousePosition))
                    {
                        if (GUI.Button(lintDisable,"",style))
                        {
                            BTEditorProperty.Instance.RemoveLine(node,child);
                            BTEditorProperty.Instance.RemoveNode(child);
                        }
                    }
                }
            }
        }

        public void AddLine(BTNode root,BTNode child)
        {
            BTLine line;
            if (!BTLineDic.TryGetValue(root,out line))
            {
                line = new BTLine();
                BTLineDic.Add(root,line);
            }
            line.ChildList.Add(child);
        }

        public void RemoveLine(BTNode root, BTNode child)
        {
            BTLine line;
            if (!BTLineDic.TryGetValue(root,out line))
            {
                return;
            }
            line.RemoveChild(child);
        }

        public void RemoveNotUseLine()
        {
            List<BTNode> needRemoves = new List<BTNode>();
            foreach (KeyValuePair<BTNode,BTLine> keyValuePair in BTLineDic)
            {
                if (keyValuePair.Value.ChildList.Count ==0 && String.CompareOrdinal(keyValuePair.Key.Label,"Root") != 0)
                {
                    needRemoves.Add(keyValuePair.Key);
                }
            }

            for (int i = 0; i < needRemoves.Count; i++)
            {
                BTLineDic.Remove(needRemoves[i]);
            }
        }
        
        public void RemoveAllLine(BTNode root)
        {
            BTLine line;
            if (!BTLineDic.TryGetValue(root,out line))
            {
                return;
            }
            line.RemoveAllChild();
        }


        public BTLine GetChilds(BTNode root)
        {
            BTLine line;
            if (BTLineDic.TryGetValue(root,out line))
            {
                return line;
            }

            return null;
        }
        
    }
}